package index

import (
	"fmt"
	"im/internal/common"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func View(c *gin.Context) {
	token := c.Query("token")
	user, _ := common.GetUserByToken(token)
	if user == nil {
		c.Redirect(http.StatusMovedPermanently, "/login")
		return
	}
	c.HTML(http.StatusOK, "chat.tmpl", gin.H{
		"token":    token,
		"username": user.Username,
		"url":      fmt.Sprintf("ws://%s:%d", viper.GetString("socket.host"), viper.GetInt("socket.port")),
	})
}
